package com.xiaomei.manager.service.impl;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaomei.manager.dao.AuthinfoMapper;
import com.xiaomei.manager.dao.BroadcastMapper;
import com.xiaomei.manager.dao.UserMapper;
import com.xiaomei.manager.model.Authinfo;
import com.xiaomei.manager.model.Broadcast;
import com.xiaomei.manager.model.JPushRecord;
import com.xiaomei.manager.model.User;
import com.xiaomei.manager.service.IAuthinfoService;
import com.xiaomei.manager.service.IJpushRecordService;
import com.xiaomei.manager.web.dto.AuthinfoAndUserDto;
import com.xiaomei.manager.web.dto.req.AuthQueryDto;
import com.xiaomei.manager.web.dto.req.DriverDto;
import com.xiaomei.manager.web.jpush.JpushUtil;
import com.xiaomei.manager.web.page.Page;
import com.xiaomei.manager.web.sms.HttpSender;



@Service
@Transactional(rollbackFor = Exception.class)
public class AuthinfoServiceImpl implements IAuthinfoService {
	
	@Autowired
	private AuthinfoMapper authinfoMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private IJpushRecordService JpushService; 
	@Autowired
	private BroadcastMapper broadcastMapper;
	private static final String XIAOMEI_CHUXING = "#老司机上路#";
	private String[]  content= new String[]{"让让让让，我来了，别挡着我接美女",
			"Hi，上我的车，开你要走的路~",
			"明天你是否会想起，昨天…你坐过我的车？",
			"小美，带我飞，顺便我带上你",
			"天气那么好，当然要坐车出去溜达溜达；天气那么糟，当然坐车回家才舒服",
			"你在人民广场吃炸鸡，我在路边停好等着你",
			"好紧张，刚注册的小美司机，怎样才能表现出老司机的状态？在线等，挺急的",
			"成为小美司机的第一天，mark一笔",
			"heyhey，旁友们，约起来，我刚注册小美司机啦",
			"来不及多说了，上车！对，我注册了小美",
			"你美你有理，你指哪儿我开哪儿"				
			};
	private String[] img= new String[]{"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img1_368X223.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img2_750X796.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img3_600X600.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img4_430X260.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img5_1024X704.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img6_349X186.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img7_620X344.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img8_600X600.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199184/img9_395X276.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199220/img1_500X533.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199220/img2_873X466.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199220/img3_313X256.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199220/img4_960X600.jpg",
			"http://xm-chuxing-app-test.oss-cn-hangzhou.aliyuncs.com/TM170227195042367133/userBroadcast/1488199220/img5_450X428.jpg"
			};
	private Random rad = new Random();
	/**
	 * 根据请求参数map查询司机认证信息
	 * @param paramMap 请求参数map
	 * @return 司机认证信息数据集
	 */
	@Override
	public List<AuthinfoAndUserDto> getAuthinfoList(AuthQueryDto paramDto,Page<AuthinfoAndUserDto> page) {
		// TODO 自动生成的方法存根
		page.setParamDto(paramDto);
		return authinfoMapper.selectByParamMap(page);
	}
	
	/**
	 * 根据定订单id查询司机认证信息
	 * @param id  
	 * @return 司机对象
	 */
	@Override
	public Object selectByPrimaryKey(Integer id) {
		// TODO 自动生成的方法存根
		return authinfoMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 司机认证根据id修改认证状态 ，0-认证中，1-认证通过，2-认证失败
	 * @param authinfo 请求参数authinfo
	 * @return 
	 */
	@Override
	public boolean updateByPrimaryKey(Authinfo authinfo) {
		User user = userMapper.selectBrodcast(authinfo.getUserCode());
		String nickname = user.getNickname();
		String mobile = user.getMobile();
		// TODO 自动生成的方法存根
		if(authinfo.getCheckState()==1){
			if(authinfoMapper.updateByPrimaryKeySelective(authinfo) > 0 ){	
				if("0".equals(authinfo.getIsImg())){
					HttpSender.sendValidCode(mobile, nickname+"大大，您的车主认证已经通过了，但是离小美男神还差一个帅帅哒头像哦");
				}
				JPushRecord jpr = new JPushRecord();
				jpr.setUserCode(authinfo.getUserCode());				
				jpr.setContent(nickname+"大大，您的车主认证已通过了，快去起开启您的新旅途吧");
				jpr.setMessageState(JpushUtil.STATE_SUCCESS);
				jpr.setMessageType(JpushUtil.DRIVER_AUTH_SUCCESS);
				jpr.setPushTime(new Timestamp(System.currentTimeMillis()));
				jpr.setReadState("0");
				JpushService.JpushService(jpr);
				//司机认证通过，发广播
				int index = rad.nextInt(content.length);
				String broadcastContent= content[index]+XIAOMEI_CHUXING;
				int index2 = rad.nextInt(img.length);
				String broadcastImg= img[index2];
				Broadcast broadcast=new Broadcast();
				broadcast.setUserCode(authinfo.getUserCode());
				broadcast.setBroadcastContent(broadcastContent);
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Timestamp ts = new Timestamp(System.currentTimeMillis());         
				String tsStr = sdf.format(ts); 
				broadcast.setReleaseTime(tsStr);
				//User selectBrodcast = userMapper.selectBrodcast(authinfo.getUserCode());
				broadcast.setBroadcastImg(broadcastImg);
				broadcast.setType("1");
				broadcastMapper.insertSelective(broadcast);
				return true;
			}		
		}else if(authinfo.getCheckState()==2){
			if(authinfoMapper.updateByPrimaryKeySelective(authinfo)>0){
				HttpSender.sendValidCode(mobile, "不好意思，您的认证无法通过，具体请联系客服");
				/*JPushRecord jpr = new JPushRecord();
				jpr.setUserCode(authinfo.getUserCode());
				jpr.setContent(JpushUtil.MESSAGE_1);
				jpr.setMessageState(JpushUtil.STATE_SUCCESS);
				jpr.setMessageType(JpushUtil.DRIVER_AUTH_FAIL);
				jpr.setPushTime(new Timestamp(System.currentTimeMillis()));
				jpr.setReadState("0");
				JpushService.JpushService(jpr);*/
				return true;
			}			
		}
		return false;
	}
	/**
	 * 司机注册量
	 */
	@Override
	public List<DriverDto> getDriver(Page<DriverDto> page) {
		// TODO Auto-generated method stub
		return authinfoMapper.selectDriver(page);
	}

	@Override
	public Object selectByPrimary(Integer id) {
		// TODO Auto-generated method stub
		return authinfoMapper.selectByPrimary(id);
	}
	
}
